<template>
  <div class="home">
    <div class="header">
      <h1>博客网</h1>
      <router-link v-if="isAuthenticated" to="/article/create" class="btn-create">
        写文章
      </router-link>
    </div>
    
    <div class="blog-list">
      <div v-if="articles.length === 0" class="no-articles">
        暂无文章，快来创建第一篇文章吧！
      </div>
      <div v-else v-for="article in articles" :key="article.id" class="article-card">
        <div class="article-header">
          <h2 class="article-title">
            <router-link :to="`/article/${article.id}`">{{ article.title }}</router-link>
          </h2>
          <div class="article-category">{{ article.category }}</div>
        </div>
        <p class="article-summary">{{ article.summary }}</p>
        <div class="article-footer">
          <span class="article-time">发布于：{{ formatDate(article.createTime) }}</span>
          <div v-if="isAuthenticated" class="article-actions">
            <router-link :to="`/article/edit/${article.id}`" class="btn-edit">编辑</router-link>
            <button @click="handleDelete(article.id)" class="btn-delete">删除</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, watch } from 'vue'
import { useStore } from 'vuex'
import { getBlogs, deleteBlog } from '../api/blog'

const store = useStore()
const articles = ref([])
const isAuthenticated = computed(() => store.state.token)

const fetchArticles = async () => {
  try {
    const response = await getBlogs()
    if (response.code === 200) {
      articles.value = response.data
      console.log('获取到文章列表：', articles.value)
    } else {
      console.error('获取文章列表失败：', response.msg)
    }
  } catch (error) {
    console.error('获取文章列表失败：', error)
  }
}

// 监听认证状态变化
watch(isAuthenticated, (newValue) => {
  if (newValue) {
    fetchArticles()
  }
})

const handleDelete = async (id) => {
  if (!confirm('确定要删除这篇文章吗？')) return
  
  try {
    await deleteBlog(id)
    await fetchArticles() // 重新获取文章列表
  } catch (error) {
    alert('删除失败：' + error.message)
  }
}

const formatDate = (date) => {
  return new Date(date).toLocaleString()
}

onMounted(() => {
  if (isAuthenticated.value) {
    fetchArticles()
  }
})
</script>

<style scoped>
.home {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.blog-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-card {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.article-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.article-title {
  margin: 0;
  font-size: 1.5em;
}

.article-title a {
  color: #1e88e5;
  text-decoration: none;
}

.article-category {
  padding: 4px 8px;
  background-color: #e3f2fd;
  color: #1e88e5;
  border-radius: 4px;
  font-size: 0.9em;
}

.article-summary {
  color: #666;
  margin: 10px 0;
  line-height: 1.6;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #eee;
}

.article-time {
  color: #999;
  font-size: 0.9em;
}

.no-articles {
  text-align: center;
  padding: 40px;
  color: #666;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.btn-create {
  padding: 8px 16px;
  background-color: #1e88e5;
  color: white;
  text-decoration: none;
  border-radius: 4px;
}

.btn-edit, .btn-delete {
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9em;
}

.btn-edit {
  background-color: #1e88e5;
  color: white;
  text-decoration: none;
  margin-right: 8px;
}

.btn-delete {
  background-color: #f44336;
  color: white;
  border: none;
}
</style> 